package controller;

import pojo.GoodsSingle;
import pojo.MyTools;
import pojo.ShopCar;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@WebServlet("/docar")
public class CarServlet extends HttpServlet {

    ArrayList<GoodsSingle> goodslist;


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        ServletContext context = getServletContext();

        String action= (String)request.getParameter("action");
        if(action==null)
            action="getgoodslist";

        HttpSession session=request.getSession();

        ShopCar myCar=(ShopCar)session.getAttribute("myCar");
        if(null==myCar)
        {
            myCar=new ShopCar();
            session.setAttribute("myCar",myCar);
        }
        switch(action){
            case "getgoodslist":

                if(null==context.getAttribute("goodslist"))
                {
                    try {
                        goodslist= MyTools.getGoods();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    context.setAttribute("goodslist",goodslist);
                }
                //转发方式调用show.jsp页面显示商品页面
                request.getRequestDispatcher("show.jsp").forward(request, response);
                break;

            case "buy":
                List<GoodsSingle> goodslist=
                        (ArrayList<GoodsSingle>)context.getAttribute("goodslist");
                int id=Integer.parseInt(request.getParameter("id"));
                GoodsSingle single=(GoodsSingle)goodslist.get(id);
                //调用ShopCar类中的addItem()方法添加商品
                myCar.addItem(single);
                request.getRequestDispatcher("show.jsp").forward(request, response);
                break;

            case "remove":
                //移除商品
                int idt = Integer.parseInt(request.getParameter("id"));	//获取商品名称
                myCar.removeItem(idt);	//调用ShopCar类中的removeItem()方法移除商品
                request.getRequestDispatcher("shopcar.jsp").forward(request, response);
                break;
            case "clear":
                //清空购物车
                myCar.clearCar();//调用ShopCar类中的clearCar()方法清空购物车
                request.getRequestDispatcher("shopcar.jsp").forward(request, response);
                break;

            default:
                request.getRequestDispatcher("show.jsp").forward(request, response);
                break;
        }

    }


    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
